class_name InteractiveBtn
extends Control

#region 属性
@export var object_text:String = ""
@export var action_text:String = "Interact"
# stay_time <= 0 表示按键直接触发
var stay_time:float
var default_scale:Vector2
var tween:Tween
var start_count:int = 0
#endregion
#region 字段
#endregion
#region 信号
signal interact_completed()
#endregion

#region 基类方法
#endregion
#region 公共方法
func init_interactive_btn(key:Key = KEY_E,stay_time:float = -1,object_text:String = "",action_text:String = ""):
	self.stay_time = stay_time
	if object_text != "":
		self.object_text = object_text
	if action_text != "":
		self.action_text = action_text
	%KeyLabel.text = OS.get_keycode_string(key)
	self.default_scale = %KeyLabel.get_parent().scale
	%ProgressBar.value = 0
	%ObjectLab.text = self.object_text
	%ActionLab.text = self.action_text
func start_interact(interactor:Interactor):
	start_count += 1
	if stay_time > 0:
		if tween and tween.is_valid():
			return
		%ProgressBar.value = 0
		%AnimationPlayer.play("interact")
		tween = create_tween()
		tween.tween_property(%ProgressBar,"value",1,stay_time)
		tween.tween_callback(func ():
			interact_completed.emit(interactor)
			end_interact(interactor)
			)
	else:
		interact_completed.emit()
		end_interact(interactor)
func end_interact(interactor:Interactor):
	if tween and tween.is_valid():
		tween.kill()
	if stay_time > 0 and start_count > 0:
		start_count -= 1
		%AnimationPlayer.play_backwards("interact")
	%KeyLabel.get_parent().scale = default_scale
	%ProgressBar.value = 0
#endregion
#region 私有方法
#endregion
#region 生命周期函数
#endregion
